﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using SD.Database;

namespace smallDeimos.Game
{
    public partial class FleetView : System.Web.UI.Page
    {
        protected string TimeCalc() { return "s"; }
        int sId;
        int pId;
        int fleetOwner;
        Dictionary<string, int> fleet;
        protected void Page_Load(object sender, EventArgs e)
        {
            ToSystemList.Attributes.Add("onchange", "Update()");
            //ToPlanet.Attributes.Add("onchange", "Update()");
            try
            {
                int UserId;
                if (Request.Cookies["sd_id"] != null)
                {
                    HttpCookie cookie2 = Request.Cookies["sd_id"];
                    UserId = int.Parse(cookie2.Value);
                }
                else
                {
                    Session["Error"] = "Cookie sd_id doesn't exists";
                    Response.Redirect("~/Error.aspx");
                    return;
                }
                sId = int.Parse(this.Request.QueryString["sId"]);
                pId = int.Parse(this.Request.QueryString["pId"]);
                fleetOwner = UserFunctions.FleetOwner(sId, pId);
                if (UserId == fleetOwner || fleetOwner == 0)
                {
                    if (fleetOwner == 0) fleetOwner = UserId;
                    fleet = SD.Database.Planets.FleetOnPlanet(sId, pId);
                    ColonyLabel.Text = fleet["ColonyShips"].ToString();
                    TransportLabel.Text = fleet["Transports"].ToString();
                    DestLabel.Text = fleet["Destroyers"].ToString();
                    CrLabel.Text = fleet["Crusiers"].ToString();
                    BsLabel.Text = fleet["Battleships"].ToString();
                    FromSys.Text = Systems.SystemName(sId).ToString() + " #" + pId.ToString();
                    FromSystem.Value = sId.ToString();
                    FromPlanet.Value  = pId.ToString();
                    EnergyField.Value = ((int)UserFunctions.GetPlayerFloat(UserId, "Energy")).ToString();
                    SpeedField.Value = ((int)UserFunctions.GetPlayerInt(UserId, "Speed")).ToString();
                }
                else
                {
                    Response.Redirect("Fleets.aspx");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (int.Parse(ColonyBox.Text) <= fleet["ColonyShips"] && int.Parse(TransportBox.Text) <= fleet["Transports"] &&
                int.Parse(DestBox.Text) <= fleet["Destroyers"] && int.Parse(CrBox.Text) <= fleet["Crusiers"] && int.Parse(BsBox.Text) <= fleet["Battleships"] &&
                (int.Parse(ColonyBox.Text) > 0 || int.Parse(TransportBox.Text) > 0 || int.Parse(DestBox.Text) > 0 || int.Parse(CrBox.Text) > 0 || int.Parse(BsBox.Text) > 0)
                )
            {
                FleetsFunctions.SentFleet(fleetOwner, sId, pId, int.Parse(ToSystemList.SelectedValue), int.Parse(ToPlanet.Text), int.Parse(ColonyBox.Text), int.Parse(TransportBox.Text),
                    int.Parse(DestBox.Text), int.Parse(CrBox.Text), int.Parse(BsBox.Text));
                Response.Redirect("Fleets.aspx");
            }
            else
            {
                Label1.Text = "Cannot send fleet";
                return;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ColonyBox.Text = ColonyLabel.Text;
            TransportBox.Text = TransportLabel.Text;
            DestBox.Text = DestLabel.Text;
            CrBox.Text = CrLabel.Text;
            BsBox.Text = BsLabel.Text;
        }
    }
}
